package com.demo;


import java.time.LocalDate;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/*
线程池实现定时任务
 */
class Demo10Counter {
    private static AtomicInteger atomInt = new AtomicInteger();
    public static int get() {
        return atomInt.incrementAndGet();
    }
}

class Demo10Task implements Runnable {
    @Override
    public void run() {
//        int id = Demo10Counter.get();
        long id = System.currentTimeMillis();
        System.out.println("start:" + id);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("end:" + id);
    }
}

public class Demo10 {
    public static void main(String[] args) {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        System.out.println("begin:" + System.currentTimeMillis());

        // 两秒后开始执行第一次任务，并在之后每隔3秒固定开始执行一次，不管前面的任务是否已执行完毕
        executorService.scheduleAtFixedRate(new Demo10Task(), 2,3, TimeUnit.SECONDS);

        // 两秒后开始执行第一次任务，之后每个任务执行完毕后再等待3秒开始执行下一次任务
        executorService.scheduleWithFixedDelay(new Demo10Task(), 2,3, TimeUnit.SECONDS);
    }
}
